<template>
  <div class="kefu-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>客服会话列表</span>
      </div>
      <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input clearable v-model="searchForm.keyword" placeholder="搜索"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>

      </el-form-item>
    </el-form>
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column prop="conversation_id" label="会话ID" width="100" />
        <el-table-column label="头像" width="100">
          <template slot-scope="{scope, row}">
            <img :src="row.avatar" alt="头像" style="width: 40px; height: 40px; border-radius: 50%;">
          </template>
        </el-table-column>
        <el-table-column prop="nickname" label="客户名称" width="180" />
        <el-table-column prop="lastMessage" label="最后一条消息" width="250" />
        <el-table-column prop="unreadCount" label="未读数量" width="100" >
          <template slot-scope="{scope, row}">
            <el-tag v-if="row.unreadCount > 0" type="danger">{{row.unreadCount}}</el-tag>
            <el-tag v-else>0</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="lastTime" label="上次消息时间" width="170" />
        <el-table-column prop="actions" label="操作">
          <template slot-scope="{scope,row}">
            <el-button type="primary" size="mini" @click="$router.push({name:'kefuChat',query:{conversation_id:row.conversation_id}})">聊天</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="fetchData"
      />
    </el-card>
  </div>
</template>

<script>
import api from '@/api/chat'
import Pagination from '@/components/Pagination'

export default {
  components: {
    Pagination
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        sort: ''
      },
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },

  methods: {
    fetchData() {
      this.listLoading = true
      api.conversations(this.listQuery).then(res => {
        this.list = res.data.list
        this.total = res.data.total
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    },

  }
}
</script>

<style scoped>
.kefu-container {
  padding: 20px;
}

.box-card {
  width: 100%;
}
</style>
